package com.bdzl.auth.service.impl;

import com.bdzl.auth.dao.RoleDao;
import com.bdzl.auth.dao.UserRoleDao;
import com.bdzl.auth.service.RoleMeauService;
import com.bdzl.auth.service.RoleService;
import com.bdzl.pub.entity.sys.RoleDO;
import com.bdzl.pub.entity.sys.RoleMeauDO;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.sys.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Slf4j
@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserRoleDao userRoleMapper;
	@Autowired
	private RoleMeauService roleMeauService;

	@Override
	public RoleDO get(Integer roleId){
		return roleDao.get(roleId);
	}

	@Override
	public RoleDO getByRoleCode(String roleCode){
		return roleDao.getByRoleCode(roleCode);
	}

	@Override
	public List<RoleDO> list(Map<String, Object> map){
		return roleDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return roleDao.count(map);
	}

	@Override
	public int save(RoleDO role){
		return roleDao.save(role);
	}

	@Override
	public R saveRoleDO(RoleVO role){
		//不是默认角色
		role.setRoleDefault("N");
		//所属机构
		role.setRoleOrg("1");
		//创建用户
		role.setRoleCreuser(1);
		if (roleDao.save(role)>0){
			RoleDO byRoleCode = getByRoleCode(role.getRoleCode());
			//选中的菜单
			List<String> menuIdList = role.getMenuIdList();
			for (String item:menuIdList) {
				if (!item.equals("-1")){
					//保存角色和菜单关系
					RoleMeauDO roleMeauDO = new RoleMeauDO();
					roleMeauDO.setMeauId(item);
					roleMeauDO.setRoleId(role.getRoleId());
					roleMeauService.save(roleMeauDO);
				}
			}
			return R.ok();
		}
		return R.error();
	}

	@Override
	public int update(RoleDO role){
		return roleDao.update(role);
	}
	@Override
	public R updateRoleDO(RoleVO role){
		//修改用户
		role.setRoleModuser(1);
		roleDao.update(role);
		//删除以前角色和菜单关系
		roleMeauService.removeByRoleId(role.getRoleId());
		//选中的菜单
		List<String> menuIdList = role.getMenuIdList();
		for (String item:menuIdList) {
			if (!item.equals("-1")){
				//保存角色和菜单关系
				RoleMeauDO roleMeauDO = new RoleMeauDO();
				roleMeauDO.setMeauId(item);
				roleMeauDO.setRoleId(role.getRoleId());
				roleMeauService.save(roleMeauDO);
			}
		}
		return R.ok();
	}

	@Override
	public int remove(Integer roleId){
		if (get(roleId).getRoleDefault().equals("Y")){
			return -10;
		}
		roleMeauService.removeByRoleId(roleId);
		return roleDao.remove(roleId);
	}

	@Override
	public int batchRemove(Integer[] roleIds){
		return roleDao.batchRemove(roleIds);
	}

	@Override
	public Set<String> getRoleByUserName(String username) {
		// 查询用户拥有的角色集合
		List<String> roles = roleDao.getRoleByUserName(username);
		log.info("-------通过数据库读取用户拥有的角色Roles------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
		assert roles != null;
		return new HashSet<>(roles);
	}

	@Override
	public List<RoleDO> userRoles(Integer userId) {
		List<Integer> rolesIds = userRoleMapper.listRoleId(userId);
		List<RoleDO> roles = roleDao.list(new HashMap<>(16));
		List<RoleDO> newroles = new ArrayList<>();
		for (int i = 0; i < rolesIds.size(); i++) {
			Integer roleid = rolesIds.get(i);
			for (RoleDO roleDO : roles) {
				if (roleDO.getRoleId().equals(roleid)) {
					newroles.add(roleDO);
					break;
				}
			}
		}
		return newroles;
	}
}
